package be.kuleuven.mume.beans;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import be.kuleuven.mume.exceptions.UserNotLoggedInException;
import be.kuleuven.mume.query.PersoonQuery;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@PersistenceCapable
public class PersoonBean {
	
	@NotPersistent
	private static Logger log = Logger.getLogger(PersoonBean.class.getName());
	//@NotPersistent
	//private static PersistenceManagerFactory pm = PMF.get();
	@NotPersistent 
	boolean debug = true;
	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key id;
	@Persistent
	private String username;
	@Persistent
	private int leeftijd;
	@Persistent(serialized = "true")
	private AccessToken twitterToken;
	@Persistent
	private List<String> vakken;
	
	public PersoonBean() {
		twitterToken = null;
		vakken = new ArrayList<String>();
	}

	public Twitter getTwitter(){
		try {
			PersoonBean pers = PersoonQuery.getCurrentPersoon("");
			if(!pers.getId().equals(this.getId())) {
				if(debug)log.log(Level.WARNING, "Twitter can only be received from current user.");
				return null;
			}
			Twitter twitter = null;
			if (this.twitterToken != null) {
				twitter = new TwitterFactory().getInstance();
				twitter.setOAuthConsumer("Sx53PNSwLsq3ifCn7ylbBw",
						"JiPdcDv7d206jpeKCZIgZOmqIMZbidSM9REGfq44");
				twitter.setOAuthAccessToken(this.twitterToken);
				
				twitter.verifyCredentials();
			}
			return twitter;
		} catch (UserNotLoggedInException e) {
			if(debug)log.log(Level.SEVERE, "UserNotLoggedIn", e);
		} catch (TwitterException e) {
			if(e.getStatusCode() == 401) {
				log.log(Level.WARNING, "TwitterException : Provided twitter credentials are not valid. Twitter token removed.");
				log.log(Level.WARNING, "TwitterException.ServerError : " + e.getErrorMessage());
				this.twitterToken = null;
			}
			if(debug)log.log(Level.SEVERE, "TwitterException", e);
		}
		return null;
	}
	
	public boolean hasTwitter() {
		if(this.twitterToken != null)
			return true;
		return false;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getUsername() {
		return username;
	}
	
	public String getMail() {
		return username + "@gmail.com";
	}

	public String getId() {
		return KeyFactory.keyToString(this.id);
	}

	public void setId(Key googleId) {
		this.id = googleId;
	}

	public void setLeeftijd(int lt) {
		this.leeftijd = lt;
	}

	public int getLeeftijd() {
		return this.leeftijd;
	}

	public AccessToken getTwitterToken(){
		return this.twitterToken;
	}
	
	public void setTwitterToken(AccessToken twitterToken) {
		this.twitterToken = twitterToken;
	}
	
	/*public void setVakken(List<String> vakken) {
		this.vakken = vakken;
	}*/
	
	public List<String> getVakken() {
		return Collections.unmodifiableList(vakken);
	}
	
	public void addVak(String vakId) throws IllegalArgumentException{
		if(vakken.contains(vakId))
			throw new IllegalArgumentException("Persoon.vakken already contains key");
		vakken.add(vakId);
	}
	
	public boolean removeVak(String vakId) throws IllegalArgumentException{
		if(this.vakken.remove(vakId))
			return true;
		else
			throw new IllegalArgumentException("Persoon.vakken vak not found");
	}
}
